﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Common;
using MyWeb.Business;
using MyWeb.Data;
namespace MyWeb.Admins
{
    public partial class Advertise : System.Web.UI.Page
    {
        static bool Insert = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["folder"] = "Advertise"; 
                BindGrid();
            }
        }
        private void BindGrid()
        {
            grdAdvertise.DataSource = AdvertiseService.Advertise_GetByAll() ;
            grdAdvertise.DataBind();
        }
        protected void btnThem_Click(object sender, EventArgs e)
        {
            pnShow.Visible = false;
            pnUpdate.Visible = true;
            ControlClass.ResetControlValues(this);
            Session["upload"] = null;Insert=true;
        }
        //protected void btnXoa_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        DataGridItem item = default(DataGridItem);
        //        for (int i = 0; i < grdAdvertise.Items.Count; i++)
        //        {
        //            item = grdAdvertise.Items[i];
        //            if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
        //            {
        //                if (((CheckBox)item.FindControl("ChkSelect")).Checked)
        //                {
        //                    string strId = item.Cells[1].Text.ToString();
        //                    try
        //                    {

        //                        AdvertiseService.Advertise_Delete(strId);
        //                    }
        //                    catch{}
        //                }
        //            }
        //        }
        //        grdAdvertise.CurrentPageIndex = 0;
        //        BindGrid();
        //    }catch{}
        //}
        protected void grdAdvertise_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            Data.AdvertiseInfo strCA=new AdvertiseInfo();
            string Id = e.CommandArgument.ToString();
            if  (e.CommandName=="Edit")
            {
                DataTable dt = AdvertiseService.Advertise_GetById(Id);
                txtId.Text=dt.Rows[0]["Id"].ToString();
                txtName.Text=dt.Rows[0]["Name"].ToString();                
                txtWidth.Text=dt.Rows[0]["Width"].ToString();
                txtHeight.Text=dt.Rows[0]["Height"].ToString();
                txtLink.Text=dt.Rows[0]["Link"].ToString();
                txtTarget.Text=dt.Rows[0]["Target"].ToString();
                ddlPosition.Text = dt.Rows[0]["Position"].ToString();
                txtOrder.Text=dt.Rows[0]["Order"].ToString();
                ddlStatus.Text=dt.Rows[0]["Status"].ToString();
                pnUpdate.Visible = true;
                pnShow.Visible = false;
            }
            else
            {
                AdvertiseService.Advertise_Delete(strCA);
                BindGrid();
            }
        }
        protected void lblBack_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;pnShow.Visible = true;
            Insert=false;
        }
        protected void lblUpdate_Click(object sender, EventArgs e)
        {
            string image = Session["upload"] == null ? "" : Session["upload"].ToString();
            #region[TestInput]            
            if (txtName.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Name không thể trống !");
                txtName.Focus();
                return;
            }
           
            if (txtWidth.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Width không thể trống !");
                txtWidth.Focus();return;}
            if (txtHeight.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Height không thể trống !");
                txtHeight.Focus();
                return;
            }
            if (txtLink.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Link không thể trống !");
                txtLink.Focus();
                return;
            }
            if (txtTarget.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Target không thể trống !");
                txtTarget.Focus();
                return;
            }
            if (ddlPosition.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Position không thể trống !");
                ddlPosition.Focus();
                return;}
            if (txtOrder.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Order không thể trống !");
                txtOrder.Focus();return;
            }
            if (ddlStatus.Text.Trim().Equals(""))
            {
                WebMsgBox.Show("Status không thể trống !");
                ddlStatus.Focus();
                return;
            }
            #endregion
            Data.AdvertiseInfo obj=new Data.AdvertiseInfo();
            obj.Id = txtId.Text;
            obj.Name = txtName.Text;
            obj.Url = image;
            obj.Width = txtWidth.Text;
            obj.Height = txtHeight.Text;
            obj.Link = txtLink.Text;
            obj.Target = txtTarget.Text;
            obj.Position = ddlPosition.Text;
            obj.Order = txtOrder.Text;
            obj.Status = ddlStatus.Text;
            if (Insert == true)
            {
                AdvertiseService.Advertise_Insert(obj);
            }
            else
            {
                uploader1.Visible = true;
                AdvertiseService.Advertise_Update(obj);
            }
            BindGrid();
            pnShow.Visible = true;
            pnUpdate.Visible = false;
            Insert = false;
}
        protected void grdAdvertise_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            grdAdvertise.CurrentPageIndex = e.NewPageIndex;BindGrid();
        }
        protected void grdAdvertise_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            ListItemType itemType = e.Item.ItemType;
            if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator))
            {
                if (itemType == ListItemType.Header)
                {
                    object checkBox = e.Item.FindControl("chkSelectAll");
                    if ((checkBox != null)){((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
                }
            }
                else
                {
                    string tableRowId = grdAdvertise.ClientID + "_row" + e.Item.ItemIndex.ToString();
                    e.Item.Attributes.Add("id", tableRowId);
                    object checkBox = e.Item.FindControl("chkSelect");
                if ((checkBox != null))
                {
                    e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
                    e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")");
                    ((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex + ")");
                }
                }
            }
        }

       

       
    }
}